#+_+ coding:utf-8 +_+

from base import BaseHdl
import auth
from storage import comment 
from common import errno
from common import buffer
import storage 
from storage import news

class CommentHdl(BaseHdl):
    def get(self):
        uid = self.get_argument("uid","")
        signature = self.get_argument("signature","")
        newsID = self.get_argument("news_id","")

        rst = auth.IsValiedUID(uid)
        if not rst:
            rep = self.uidError()
        else:
            ticket = storage.uid.UID.GetTicket(uid)
            rst = auth.CheckSignature(signature,uid,newsID,ticket)
            if not rst :
                rep = self.signatureError()
            else:
                rst = news.CheckNewsID(newsID)
                if not rst :
                    rep  = self.newsIDError()
                else:
                    rep = self.comments(newsID)
        self.write(rep)

    def newsIDError(self):
        msg = dict(errno=errno.ENEWS_ID_NOTVALIED,errmsg=errno.ErrStr(errno.ENEWS_ID_NOTVALIED))
        rep = dict(rst="FAILED",msg=msg)
        return buffer.JsonEncode(rep)

    def commentIDError(self):
        msg = dict(errno=errno.ECOMMENT_ID_NOTVALIED,errmsg=errno.ErrStr(errno.ECOMMENT_ID_NOTVALIED))
        rep = dict(rst="FAILED",msg=msg)
        return buffer.JsonEncode(rep)
    
    def comments(self,newsID):
        comments = comment.Get(newsID)
        count = len(comments)
        msg = dict(count=count,comments=comments)
        rep = dict(rst="SUCCESS",msg =msg)
        return buffer.JsonEncode(rep)

        


